import sqlite3
from mapper.user_mapper import *
from entity.OperateLog import *
from tool.my_uuid import get_uuid

user_mapper: User_mapper = User_mapper("./db/data.db")


class Log_Mapper:
    def __init__(self, db_path):  # 构造方法
        self.conn = sqlite3.connect(db_path)

    def __del__(self):  # 销毁勾子
        self.conn.close()

    def __save_log(self, user_name: str, level: str, operate: str):
        if user_name == "" or level == "" or operate == "": return 0

        print(f"【用户登录】:{user_name}")

        cursor = self.conn.cursor()
        timestamp = int(time.time())
        uuid: str = get_uuid()
        cursor.execute("INSERT INTO operate_Log(id,user_name,level,operate,create_time) "
                       "VALUES(?,?,?,?,?)", (uuid, user_name, level, operate, timestamp))
        self.conn.commit()

    def save_info_log(self, user_name: str, operate: str):
        self.__save_log(user_name, "INFO", operate)

    def save_warn_log(self, user_name: str, operate: str):
        self.__save_log(user_name, "WARN", operate)

    def save_error_log(self, user_name: str, operate: str):
        self.__save_log(user_name, "ERROR", operate)
